package classification.imp;

import classification.PaymentClassification;
import domain.Employee;
import domain.TimeCard;
import exception.EmployeeException;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HourlyClassification implements PaymentClassification {
    private double hourlyRate;
    private Map<Date, TimeCard> timeCardMap = new HashMap<>();

    public HourlyClassification(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public void addTimeCard(TimeCard timeCard) {
        timeCardMap.put(timeCard.getDate(), timeCard);
    }

    public TimeCard getTimeCard(Date date) throws EmployeeException {
        TimeCard timeCard = timeCardMap.get(date);
        if(timeCard == null) {
            throw new EmployeeException("no such timecart: " + date.toString());
        }
        return timeCard;
    }

    @Override
    public double calcGrossPay() {
        double hours = 0;
        for (Map.Entry<Date, TimeCard> entry : timeCardMap.entrySet()) {
            hours += entry.getValue().getHours();
        }
        timeCardMap.clear();
        return hours * hourlyRate;
    }
}
